<template>
    <div class="FromGood">
        <el-form :model="album" label-width="120px" class="demo-ruleForm">
            <el-form-item label="相册名称" prop="name">
                <el-input v-model="album.albumName" placeholder="相册名称"></el-input>
            </el-form-item>
            <el-form-item label="内容描述" prop="albumDescription">
                <el-input type="textarea" :rows="3" v-model="album.albumDescription"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="order">
                <el-input  v-model="album.order"></el-input>
            </el-form-item>
            <el-form-item label="照片列表" prop="region">
                <el-upload action="#" ref="upload" :on-preview="handlePreview" :on-remove="handleRemove"
                    list-type="picture-card" :file-list="fileList" :http-request="handleFileUpload">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="updateDetail()">保存</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { AlbumUpload,deleteFile} from '@/assets/api/fileUpload.js';
import { selectAlbumDetail, updateAlbumDetail } from '@/assets/api/album.js'
export default {
    data() {
        return {
            imageList: [],
            fileList: [],
            albumId: '',
            album: {
                albumName: '',
                albumDescription: '',
                order: '',
                albumPhotos: '',
        
            },

            dialogImageUrl: '',
            dialogVisible: false

        }
    },
    mounted() {
        this.albumId = this.$route.params.albumId
        if (this.albumId != undefined && this.albumId != '') {
            this.showDetail()
        }
    },
    methods: {

        async showDetail() {
            selectAlbumDetail(this.albumId).then((resp) => {
                console.log(resp)
                if (resp.code == 200) {
                    this.album = resp.data;
                    if (this.album.albumPhotos != '') {
                        this.imageList = this.album.albumPhotos.split(",")
                    }
                    if (resp.data.imageList.length > 0) {
                        for (var i = 0; i < resp.data.imageList.length; i++) {
                            this.fileList.push({ "name": resp.data.imageList[i].pictureId
                            ,"url": resp.data.imageList[i].pictureUrl })
                        }
                    }
                }
            })
        },

        async updateDetail() {
            if (this.imageList.length > 0) {
                this.album.albumPhotos =  this.imageList.toString();;
            }
            updateAlbumDetail(data).then((resp) => {
                if (resp.code == 200) {
                    //关闭当前卡签页
                    this.$message.success("保存成功");
                    this.$store.dispatch("delVisitedViews", this.$route)
                    this.$router.push("/album/albumList")
                }else{
                    this.$message.error(resp.message);
                }
            })
        },
        // 处理移除操作
        handleRemove(file, fileList) {
            var fileName=file.name;
            deleteFile(fileName).then((resp)=>{
                if(resp.code==200){
                    this.imageList=this.imageList.filter(item=>item!=fileName)
                }
            })
        },
        // 处理预览操作
        handlePreview(file) {
            console.log(file);
        },
        // 处理文件上传操作
        handleFileUpload(file) {
            this.loading = true;
            var form = new FormData();
            // 添加键值对
            form.append("file", file.file);
            // 调用后端服务器的接口
            AlbumUpload(form).then((resp) => {
                this.dialogImageUrl = resp.data.pictureUrl;
                this.imageList.push(resp.data.pictureId)
               
            }).catch((e) => {
                this.$message.error(e.message);
                this.$refs.upload.clearFiles();
            })
        }
    }
}
</script>

<style scoped>
.FromGood {
    width: 100%;
    min-height: 100%;
    padding: 15px;
    box-sizing: border-box;
}
</style>